package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.AliyunOSSUtil;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * @author wanghaotian
 * time:2022/12/20,0020 17:55
 * description:
 */

@Service
public class DtsStorageServiceImpl implements DtsStorageService {

    @Autowired
    private DtsStorageMapper storageMapper;

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Override
    public IPage<DtsStorage> findPage(Integer page, Integer limit, String name, String key, String sort, String order) {
        IPage<DtsStorage> iPage = new Page<>(page,limit);
        QueryWrapper<DtsStorage> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(key)) {
            wrapper.like("`key`", key);
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if (order.equals("desc")){
                wrapper.orderByDesc(sort);
            }else {
                wrapper.orderByAsc(sort);
            }
        }
        iPage = storageMapper.selectPage(iPage, wrapper);
        return iPage;
    }

    @Override
    public DtsStorage save(DtsStorage storage, byte[] bytes) throws Exception {
        String uuid = UUID.randomUUID().toString();
        String key = uuid + "-" + storage.getName();
        String url = AliyunOSSUtil.uploadImage(endpoint, accessKeyId, accessKeySecret, bucketName, key, bytes);
        storage.setKey(key);
        storage.setUrl(url);
        storage.setAddTime(new Date());
        storage.setUpdateTime(new Date());
        storage.setDeleted(false);
        storageMapper.insert(storage);
        return storage;
    }

    @Override
    public void delete(DtsStorage storage) {
        AliyunOSSUtil.deleteFile(endpoint, accessKeyId, accessKeySecret, bucketName, storage.getKey());
        storageMapper.deleteById(storage);
    }

    @Override
    public DtsStorage update(DtsStorage storage) {
        if(storage.getName() == null){
            throw new RuntimeException("图片名不能为空");
        }
        storage.setUpdateTime(new Date());
        storageMapper.updateById(storage);
        return storage;
    }
}
